<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
	String path = request.getContextPath();
%>
<html>
	<head>
		<title>群组分类维护</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/btn_class.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/ext-3.2.0/resources/css/ext-all.css" />

	<script type="text/javascript" src="<%=path%>/ext-3.2.0/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="<%=path%>/ext-3.2.0/ext-all.js"></script>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	
</head>

<body >
<script type="text/javascript">
Ext.BLANK_IMAGE_URL = "<%=path%>/ext-3.2.0/resources/images/default/s.gif";
Ext.onReady(function(){
	
	var Jthtwb_office = Ext.data.Record.create([ {
		name : 'fid',
		mapping : 'fid',
		type : 'string'
	}, {
		name : 'fname',
		mapping : 'fname',
		type : 'string'
	}, {
		name : 'fdesc',
		mapping : 'fdesc',
		type : 'string'
	}, {
		name : 'fopdate',
		mapping : 'fopdate',
		type : 'string'
	}, {
		name : 'fflag',
		mapping : 'fflag',
		type : 'string'
	}, {
		name : 'fcode',
		mapping : 'fcode',
		type : 'string'
	}]);

	var ds_officeType = new Ext.data.Store({
		reader : new Ext.data.JsonReader({
			totalProperty : 'totalProperty',
			root : 'root'
		}, Jthtwb_office),
		idProperty : 'threadid',
		remoteSort : true,
		url : 'officeInfo!queryOfficeType.do'
	});

	ds_officeType.load({
		params : {
			start : 0,
			limit : 50
		}
	});


	//定义勾选框，不需要可不必定义
	var sm_officeType = new Ext.grid.CheckboxSelectionModel({
		singleSelect : true
	});

	//定义列
	var colM_officeType = new Ext.grid.ColumnModel([ sm_officeType, new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
	    {
			header : '科室类别名称',
			dataIndex : 'fname',
			width : 150,
			renderer : function(value) {
				if (value == "null") {
					return "";
				} else {
					return value;
				}
			}
		}, {
			header : '备注',
			dataIndex : 'fdesc',
			width : 400,
			renderer : function(value) {
					if (value == "null") {
						return "";
					} else {
						return value;
					}
				}
		} ]);

	//生成表格
	var grid_officeType = new Ext.grid.GridPanel({
		closable : false,
		region : 'center',
		height : 350,
		autoWidth : true,
		cm : colM_officeType,
		ds : ds_officeType,
		sm : sm_officeType,
		enableHdMenu : false
	});
	
	
	var btn_add = new Ext.Button({
		text : '新增',
		iconCls : 'sy-add',
		handler : function() {
			officeType_detail_form();
		}
	});

	function officeType_detail_form() {
		
		var officeType_form_send = new Ext.form.FormPanel({  
	        frame : true,  
			labelWidth : 55,
	        waitMsgTarget: true,  
	        items : [{
				xtype : 'textfield',
				fieldLabel : '类别名称',
				id : 'fname_id',
				name : 'fname',
				allowBlank : false,
				width : 200
			},{
				xtype : 'textarea',
				fieldLabel : '备注',
				id : 'fdesc_id',
				name : 'fdesc',
				height : 60,
				width : 240
		}]});
			
		var btn_save_form = new Ext.Button({
			text : '保存',
			iconCls : 'sy-save',
			handler : function(btn) {
				var frm = officeType_form_send.getForm();
				
				if (frm.isValid()) {
					frm.submit({
					    url : 'officeInfo!saveOfficeType.do',
					    method : 'POST',
						waitTitle : '请稍候',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
								ds_officeType.reload();
								officeType_detail_window.close();
						},
						failure : function(form, action) {
							
						},// failure
						icon : Ext.Msg.ERROR
					});
				};// isValid
			}
		});
		
		var btn_reset_form = new Ext.Button({
			text : '取消',
			iconCls : 'sy-reset',
			handler : function(btn) {
				officeType_detail_window.close();
			}

		});

		var officeType_detail_window = new Ext.Window({
			tbar : [ btn_save_form, '-', btn_reset_form],
			width : 340,
			height : 200,
			title : '新增科室类别',
			iconCls : 'sy-win-flag',
			modal : true,
			items : [ officeType_form_send ]
		});
		officeType_detail_window.show();
	} // end form

	
	
	var btn_op_office = new Ext.Button({
		text : '科室分类',
		iconCls : 'sy-table_relationship',
		handler : function() {
			var record = grid_officeType.getSelectionModel().getSelected();
			if(record) {
				opOffice_fun(record.data.fid);
			} else {
				Ext.MessageBox.alert('提示', '请选择科室类别！');
			}
		}
	});
	
	
	function opOffice_fun(fidStr) {

		var ds_officeUnChecked = new Ext.data.Store({
			reader : new Ext.data.JsonReader({
				totalProperty : 'totalProperty',
				root : 'root'
			}, Jthtwb_office),
			idProperty : 'threadid',
			remoteSort : true,
			url : 'officeInfo!queryOfficeUnChecked.do'
		});

		ds_officeUnChecked.load({
			params : {
				'fofficeType' : fidStr
			}
		});
		
		var ds_officeChecked = new Ext.data.Store({
			reader : new Ext.data.JsonReader({
				totalProperty : 'totalProperty',
				fields : fields,
				root : 'root'
			}, Jthtwb_office),
			idProperty : 'threadid',
			remoteSort : true,
			url : 'officeInfo!queryOfficeChecked.do'
		});

		ds_officeChecked.load({
			params : {
				'fofficeType' : fidStr
			}
		});
		
			// Generic fields array to use in both store defs.
			var fields = [
			   {name: 'fname', mapping : 'fname'},
			   {name: 'fcode', mapping : 'fcode'}
			];
			

			// Column Model shortcut array
			var cols = [new Ext.grid.RowNumberer(),
				{ id : 'name', header: "科室名称", width: 300, sortable: true, dataIndex: 'fname'}
			];
		    
			// declare the source Grid
		    var firstGrid = new Ext.grid.GridPanel({
				ddGroup          : 'secondGridDDGroup',
		        store            : ds_officeUnChecked,
		        columns          : cols,
				enableDragDrop   : true,
		        stripeRows       : true,
		        autoExpandColumn : 'name',
		        width            : 300,
				region           : 'west',
		        title            : '待选科室列表'
		    });

			
		    // create the destination Grid
		    var secondGrid = new Ext.grid.GridPanel({
				ddGroup          : 'firstGridDDGroup',
		        store            : ds_officeChecked,
		        columns          : cols,
				enableDragDrop   : true,
		        stripeRows       : true,
		        autoExpandColumn : 'name',
		        width            : 300,
				region           : 'center',
		        title            : '已选科室列表'
		    });

			
			//Simple 'border layout' panel to house both grids
			var displayPanel = new Ext.Panel({
				width    : 650,
				height   : 300,
				layout   : 'border',
				items    : [
					firstGrid,
					secondGrid
				]
			});

			// used to add records to the destination stores
			var blankRecord =  Ext.data.Record.create(fields);
				
			
		var btn_save_form = new Ext.Button({
			text : '保存',
			iconCls : 'sy-sure',
			handler : function(btn) {
				var fofficeCode = "";
				for (var i = 0; i < ds_officeChecked.getCount(); i++) {
					var record = ds_officeChecked.getAt(i);
					fofficeCode += "" + record.data.fcode + "," 
				}
				
				Ext.Ajax.request({
					url : 'officeInfo!updateOfficeTypeRela.do',
					params:{
	    	        	"fofficeCode" : fofficeCode,
	    	        	"fofficeType" : fidStr
		    	    },
					success : function() {
						officeType_detail_window.close();
						Ext.MessageBox.alert('提示', '保存成功！');
					},
					failure : function() {
						Ext.Msg.show({
							title : '错误提示',
							msg : '失败！',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
						});
					}
				});
			}
		});
		
		var btn_reset_form = new Ext.Button({
			text : '取消',
			iconCls : 'sy-cancel',
			handler : function(btn) {
				officeType_detail_window.close();
			}

		});

		var officeType_detail_window = new Ext.Window({
			tbar : [ btn_save_form, '-', btn_reset_form],
			width : document.body.offsetWidth,
			height : document.body.offsetHeight,
			layout : 'fit',
			title : '科室分类',
			iconCls : 'sy-win-flag',
			modal : true,
			items : [ displayPanel ]
		});
		officeType_detail_window.show();
		
		
		/****
		* Setup Drop Targets
		***/
		// This will make sure we only drop to the view container
		var firstGridDropTargetEl =  firstGrid.getView().el.dom.childNodes[0].childNodes[1];
		var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
			ddGroup    : 'firstGridDDGroup',
			copy       : true,
			notifyDrop : function(ddSource, e, data){
				
				function addRow(record, index, allItems) {
					var foundItem = ds_officeUnChecked.find('fname', record.data.fname);
					if (foundItem  == -1) {
						ds_officeUnChecked.add(record);
						//ds_officeUnChecked.sort('fname', 'ASC');
						ddSource.grid.store.remove(record);
					}
				}
				Ext.each(ddSource.dragData.selections ,addRow);
				return(true);
			}
		}); 	

		var secondGridDropTargetEl = secondGrid.getView().el.dom.childNodes[0].childNodes[1]
		
		var destGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
			ddGroup    : 'secondGridDDGroup',
			copy       : false,
			notifyDrop : function(ddSource, e, data){
				function addRow(record, index, allItems) {
					
					var foundItem = ds_officeChecked.find('fname', record.data.fname);
					if (foundItem  == -1) {
						ds_officeChecked.add(record);
						//ds_officeChecked.sort('fname', 'ASC');
						ddSource.grid.store.remove(record);
					}
				}
				Ext.each(ddSource.dragData.selections ,addRow);
				return(true);
			}
		});
		
		
		
	} // end form

	var btn_delete = new Ext.Button({
		text : '删除',
		iconCls : 'sy-delete',
		handler : function() {
			var record = grid_officeType.getSelectionModel().getSelected();
			if (record) {
				Ext.Msg.confirm('确认删除', '您确定删除该科室类别吗？', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url : 'officeInfo!deleteOfficeType.do',
							params:{
			    	        	"fid" : record.data.fid
				    	    },
							success : function() {
								ds_officeType.removeAll();
								ds_officeType.reload();
								Ext.MessageBox.alert('提示', '删除成功！');
							},
							failure : function() {
								Ext.Msg.show({
									title : '错误提示',
									msg : '删除记录失败！',
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.ERROR
								});
							}
						});
					}
				});
			} else {
				Ext.MessageBox.alert('提示', '请选择一行纪录进行操作！');
				return;
			}
		}
	});
	
	var btn_refresh = new Ext.Button({
		text : '刷新',
		iconCls : 'sy-refresh',
		handler : function() {
			ds_mould.removeAll();
			ds_mould.reload();
		}
	});
	
    var panel = new Ext.Panel({
    	region: "center",
		layout:'fit',
		border : true,
	    //title:'&nbsp;&nbsp;&nbsp;图表示例',
	    //collapsible : true,
		margins:'0 5 0 0',
		tbar : [ btn_add, '-', btn_delete, '-', btn_op_office ],
		items : [ grid_officeType]
	});
    
 	new Ext.Viewport( {
		layout : "border",
		items : [panel]
	});
});
</script>

</body>
</html>
